class Solution:
    def strToInt(self, str: str) -> int:
        str = str.strip(' ')
        res = 0
        flag = 1
        hasFlag = False
        for i in str:
            if(i == '-'):
                if(hasFlag):
                    break
                flag = flag*-1
                hasFlag = True
                continue
            if(i == '+'):
                if(hasFlag):
                    break
                hasFlag = True
                continue
            if(i < '0' or i > '9'):
                break

            else:
                hasFlag = True
                c = ord(i)-48
                res = res*10+c
        res = res*flag
        if(res > 2147483647):
            return 2147483647
        elif(res < -2147483648):
            return -2147483648
        else:
            return res


if __name__ == "__main__":
    print(Solution().strToInt('42'))
    print(Solution().strToInt("-5-"))
    print(Solution().strToInt('+-2'))
    print(Solution().strToInt('   +0 123'))
    print(Solution().strToInt('4193 with words'))
    print(Solution().strToInt('   -42'))
    print(Solution().strToInt('words and 987'))
    print(Solution().strToInt('-91283472332'))
    print(Solution().strToInt("0-1"))
